package BasicClass.Four;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Test_Seventeenth{
    public static void main(String[] args) throws IOException {
        BufferedReader buf= new BufferedReader(new InputStreamReader(System.in));
        String[] strs = buf.readLine().split(",");
        ListNode head = new ListNode(0);
        ListNode temp = head;
        for (int i = 0; i < strs.length; i++) {
            temp.next = new ListNode(Integer.parseInt(strs[i]));
            temp = temp.next;
        }

        ListNode list = detectCycle(head.next);
//        System.out.println(list.val+"-->");
    }

    public static ListNode detectCycle(ListNode head) {
        if (head == null){
            return null;
        }
        ListNode fastNode = head;
        ListNode lastNode = head;
        while (fastNode != null && fastNode.next!=null){
            fastNode = fastNode.next.next;
            lastNode = lastNode.next;
            if(fastNode == lastNode){
                fastNode = head;
                while (fastNode != lastNode){
                    fastNode = fastNode.next;
                    lastNode = lastNode.next;
                }
                return fastNode;
            }
        }
        return null;
    }

    static class ListNode {
        int val;
        ListNode next;
        ListNode(int x) {
            val = x;
            next = null;
        }
    }
}
